package com.zyy.jomap.back;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * OK   使用javaparser解析方法体内的变量和变量类型。User.class和三方jar包中的实体还是需要通过import来识别
 * @author zhangyy
 */
public class ResolveMain {

    private static final String SRC_PATH = "E:/iot-workspace/yjomap-demo/src/main/java";
    //private static final String JAVA_PATH=SRC_PATH+"/com/zyy/yjomapdemo/bo/ProjectBO.java";
    private static final String JAVA_PATH=SRC_PATH+"/com/zyy/yjomapdemo/controller/ConvertController.java";
    public static void main_ok(String[] args) {
        TypeSolver reflectionType=new ReflectionTypeSolver();
        TypeSolver javaParserType=new JavaParserTypeSolver(new File(SRC_PATH));
        //reflectionType.setParent(reflectionType);
        CombinedTypeSolver combinedTypeSolver=new CombinedTypeSolver();
        combinedTypeSolver.add(reflectionType);
        combinedTypeSolver.add(javaParserType);
//        try {
//            combinedTypeSolver.add(JarTypeSolver.getJarTypeSolver("E:/iot-workspace/common/target/common-1.0.0-SNAPSHOT.jar"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        JavaSymbolSolver symbolSolver=new JavaSymbolSolver(combinedTypeSolver);
        StaticJavaParser
                .getConfiguration()
                .setSymbolResolver(symbolSolver);

        try {
            CompilationUnit cu = StaticJavaParser.parse(new File(JAVA_PATH));
            List<MethodDeclaration> methodDeclarationList=cu.findAll(MethodDeclaration.class);
            for(MethodDeclaration methodDeclaration:methodDeclarationList){
                List<VariableDeclarator> variableDeclaratorList= methodDeclaration.findAll(VariableDeclarator.class);
                for(VariableDeclarator variableDeclarator:variableDeclaratorList){
                    if(variableDeclarator.getType().resolve().isReferenceType()){
                        try {
                            System.out.println(variableDeclarator.getName() + "====>>>>>>" + variableDeclarator.getType().resolve().asReferenceType().getQualifiedName());
                        }catch(Exception e){
                        }
                    }
                }
            }
//            FieldDeclaration fieldDeclaration = Navigator.findNodeOfGivenClass(cu, FieldDeclaration.class);
//
//            NodeList<VariableDeclarator> variableList=fieldDeclaration.getVariables();
            //                System.out.println("Field type: " + fieldDeclaration.getVariables().get(0).getType()
//                        .resolve().asReferenceType().getQualifiedName());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main32131(String[] args) {
        TypeSolver reflectionType=new ReflectionTypeSolver();
        TypeSolver javaParserType=new JavaParserTypeSolver(new File(SRC_PATH));
        CombinedTypeSolver combinedTypeSolver=new CombinedTypeSolver();
        combinedTypeSolver.add(reflectionType);
        combinedTypeSolver.add(javaParserType);
        JavaSymbolSolver symbolSolver=new JavaSymbolSolver(combinedTypeSolver);
        JavaParser javaParser=new JavaParser();
        javaParser.getParserConfiguration().setSymbolResolver(symbolSolver);

        try {
            ParseResult<CompilationUnit>  result = javaParser.parse(new File(JAVA_PATH));
            List<MethodDeclaration> methodDeclarationList=result.getResult().get().findAll(MethodDeclaration.class);
            for(MethodDeclaration methodDeclaration:methodDeclarationList){
                List<VariableDeclarator> variableDeclaratorList= methodDeclaration.findAll(VariableDeclarator.class);
                for(VariableDeclarator variableDeclarator:variableDeclaratorList){
                    try {
                        if(variableDeclarator.getType().resolve().isReferenceType()){
                            System.out.println(variableDeclarator.getName() + "==+++++++>>" + variableDeclarator.getType().resolve().asReferenceType().getQualifiedName());
                        }
                    }catch (Exception e){
                        try {
                            System.out.println(variableDeclarator.getName()+"............com.hoolink.iot.common.server.device.dmx."+variableDeclarator.getType().toString());
                            Class clazz=ResolveMain.class.getClassLoader().loadClass("com.hoolink.iot.common.server.device.dmx."+variableDeclarator.getType().toString());
                            for(Field field:clazz.getDeclaredFields()){
                                System.out.println("dfadfsa+++++++++++++++"+field.getName());
                            }
                        } catch (ClassNotFoundException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
